﻿namespace MicroCloud.Audits
{
    /// <summary>
    /// 审计实体事件数据 <see cref="AuditEntityEntry"/>
    /// </summary>
    public class AuditEntityEventData : EventDataBase
    {
        #region "属性"
        /// <summary>
        /// 获取 审计实体条目数据集合
        /// </summary>
        public IEnumerable<AuditEntityEntry> AuditEntries { get; private set; }

        #endregion

        #region "构造函数"
        #region "初始化一个审计实体事件数据 <see cref="AuditEntityEventData"/> 的新实例"
        /// <summary>
        /// 初始化一个审计实体事件数据 <see cref="AuditEntityEventData"/> 的新实例
        /// </summary>
        /// <param name="auditEntries">审计实体条目数据集合</param>
        public AuditEntityEventData(IList<AuditEntityEntry> auditEntries)
        {
            Check.NotNull(auditEntries, nameof(auditEntries));

            AuditEntries = auditEntries;
        }
        #endregion

        #endregion

    }

    /// <summary>
    /// 审计实体事件处理器
    /// </summary>
    public class AuditEntityEventHandler : EventHandlerBase<AuditEntityEventData>
    {
        #region "字段"
        #region "基于Scoped生命周期的数据字典，可用于在Scoped环境中传递数据"
        /// <summary>
        /// 基于Scoped生命周期的数据字典，可用于在Scoped环境中传递数据
        /// </summary>
        private readonly ScopedDictionary _scopedDictionary;
        #endregion

        #endregion

        #region "构造函数"
        #region "初始化一个审计实体事件处理器 <see cref="AuditEntityEventHandler"/> 的新实例"
        /// <summary>
        /// 初始化一个审计实体事件处理器 <see cref="AuditEntityEventHandler"/> 的新实例
        /// </summary>
        /// <param name="scopedDictionary"></param>
        public AuditEntityEventHandler(ScopedDictionary scopedDictionary)
        {
            _scopedDictionary = scopedDictionary;
        }
        #endregion

        #endregion

        #region "方法"
        #region "事件处理"
        /// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="eventData">事件源数据</param>
        /// <param name="provider">服务提供者</param>
        public override void Handle(AuditEntityEventData eventData, IServiceProvider provider)
        {
            eventData.CheckNotNull(nameof(eventData));

            AuditOperationEntry operation = _scopedDictionary.AuditOperation;
            if (operation == null)
            {
                return;
            }
            foreach (AuditEntityEntry auditEntity in eventData.AuditEntries)
            {
                SetAddedId(auditEntity);
                operation.EntityEntries.Add(auditEntity);
            }
        }
        #endregion

        #endregion

        #region "私有静态方法"
        #region "设置添加的Id"
        /// <summary>
        /// 设置添加的Id
        /// </summary>
        /// <param name="entry">审计实体信息</param>
        private static void SetAddedId(AuditEntityEntry entry)
        {
            if (entry.OperateType == OperateType.Insert)
            {
                dynamic entity = entry.Entity;
                entry.EntityKey = entity.Id.ToString();
                AuditPropertyEntry property = entry.PropertyEntries.FirstOrDefault(m => m.FieldName == "Id");
                if (property != null)
                {
                    property.NewValue = entity.Id.ToString();
                }
            }
        }
        #endregion

        #endregion

    }

}
